#pragma once
#ifndef __INVERTEDINDEX_H__
#define __INVERTEDINDEX_H__
#include "WebPage.h"
#include "Configuration.h"
#include "SplitTool.h"
#include <string>
#include <unordered_map>
#include <vector>
#include <set>

using std::vector;
using std::string;
using std::set; 
using std::unordered_map;
using std::pair;

class InvertedIndex
{
public:
    InvertedIndex(SplitTool* tool,const string& confPath,
                   const string& cleanPageName,
                   const string& noDupIndexName,
                   const string& invertedIndexName)
    :_tool(tool)
    ,_conf(confPath)
    ,_cleanedPageName(cleanPageName)
    ,_noDupIndexName(noDupIndexName)
    ,_invertIndexName(invertedIndexName)
    {}

    void buildStopSet();    //获得停用词集合
    void buildOffsetLib(); 
    void parsePaper(string paper,WebPage& webPage);
    void getWebPage(int docid,string & paper);
    void buildPreInfo(WebPage webPage);
    void buildInvertedIndex();
    void store(); //将倒排索引存入磁盘
    void start();
private:
    SplitTool* _tool;
    Configuration _conf;
    string _cleanedPageName;
    string _noDupIndexName;
    string _invertIndexName;

    WebPage _webPage;       //保存一篇文档
    //去重后的文档偏移库,int为docid,第一个int为起始位置,第二个int为文档长度
    unordered_map<int,pair<int,int>> _offsetLib;
    //int为docid,string为单词/词语,int为该单词在此文章中出现的次数
    unordered_map<int,unordered_map<string,int>> _TF;  
    unordered_map<string,int> _DF;  //string为单词,int表示出现该单词的文档数
    set<string> _stopWords; //停用词的集合
    //倒排索引,string表示单词/词语,int为docid,double为权重
    unordered_map<string,set<pair<int,double>>> _invertIndex;
};

#endif
